package com.hzlj.position.locate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hzlj.position.config.cache.JzJbxxDeviceBindCache;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindCacheDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindUpdateDTO;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.differentiation.positionWarningNotice.PositionWarningNotice;
import com.hzlj.position.locate.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lifh
 * @date 2022/10/9 13:32
 **/
@Slf4j
@Service
public class PositionAddServiceImpl implements PositionAddService {
    @Resource
    private PositionService positionService;
    @Resource
    private PositionNewestService positionNewestService;
    @Resource
    private PositionNewestEffectiveService positionNewestEffectiveService;
    @Resource
    private JzJbxxDeviceBindCache jzJbxxDeviceBindCache;
    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;
    @Resource
    private AbnService abnService;
    @Resource
    private PositionWarningNotice positionWarningNotice;
    @Resource
    private ReportInspectionSyncService reportInspectionSyncService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(List<Position> positions) {
        //去掉定位失败
        List<Position> positionsWithoutFailLocate = positions.stream()
                .filter(e -> ObjectUtil.isAllNotEmpty(e.getLongitude(), e.getLatitude()))
                .collect(Collectors.toList());

        //需要更新首次定位时间的记录
        List<JzJbxxDeviceBindUpdateDTO> deviceBindUpdateFirstLocateTimes = positionsWithoutFailLocate.stream().map(e -> {
            JzJbxxDeviceBindCacheDTO deviceBindCache = jzJbxxDeviceBindCache.getJzJbxxDeviceBindCache(e.getDeviceCode(), e.getChannel());
            return deviceBindCache.getFirstLocateTime() != null ?
                    null : new JzJbxxDeviceBindUpdateDTO().setId(deviceBindCache.getId()).setFirstLocateTime(e.getPositionTime());
        }).filter(Objects::nonNull).collect(Collectors.toList());

        //保存定位记录
        positionService.saveBatch(positions);
        //保存最新记录
        positionNewestService.saveBatch(positions);
        //保存最新记录(有效记录)
        positionNewestEffectiveService.saveBatch(positionsWithoutFailLocate);
        //更新首次定位时间
        jzJbxxDeviceBindService.updateFirstLocateTime(deviceBindUpdateFirstLocateTimes);
        //分析位置信息
        abnService.syncPositions(positionsWithoutFailLocate);
        //发送异常通知
        positionWarningNotice.noticeJzry(positions);
        positionWarningNotice.noticeGzry(positions);
        //发出位置确认通知
//        reportInspectionSyncService.syncReportInspectionTrackCheck(positions);
    }
}
